package com.yang.doctorrecruitment.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.MessageDAO;
import com.yang.doctorrecruitment.dto.Message;
import com.yang.doctorrecruitment.dto.UserLogin;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class GetMessageAction extends ActionSupport {
    //收件，多条，json存储每条信息，此外还需要插入
    private JSONArray news;
    private JSONArray user;

    public String GetReceiveMessage(){
        HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String UserID=request.getParameter("UserID");
        //查询ToUserID=UserID的
        List<Message> list= MessageDAO.selectToUserID(UserID);//收件箱
        //要不要补充其他的信息？补充一下吧，用户呢称即可
        JSONArray messages= JSONArray.parseArray(JSON.toJSONString(list));
        List<UserLogin> username=MessageDAO.selectUserName(UserID);
        JSONArray users= JSONArray.parseArray(JSON.toJSONString(username));

        news=messages;
        user=users;
        return SUCCESS;
    }

    public String GetPostMessage(){
        HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String UserID=request.getParameter("UserID");
        //查询ToUserID=UserID的
        List<Message> list= MessageDAO.selectUserID(UserID);//收件箱
        //要不要补充其他的信息？补充一下吧，用户呢称即可
        JSONArray messages= JSONArray.parseArray(JSON.toJSONString(list));
        List<UserLogin> username=MessageDAO.selectReceiveUserName(UserID);
        JSONArray users= JSONArray.parseArray(JSON.toJSONString(username));

        news=messages;
        user=users;
        return SUCCESS;
    }

    public JSONArray getNews() {
        return news;
    }

    public void setNews(JSONArray news) {
        this.news = news;
    }

    public JSONArray getUser() {
        return user;
    }

    public void setUser(JSONArray user) {
        this.user = user;
    }
}
